package com.zhkj.txg.module.order.adapter

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.marginTop
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.LogUtils
import com.chad.library.adapter.base.BaseNodeAdapter
import com.chad.library.adapter.base.entity.node.BaseNode
import com.chad.library.adapter.base.provider.BaseNodeProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zhkj.lib.base.BaseApplication
import com.zhkj.lib.utils.ScreenUtil
import com.zhkj.lib.utils.SpanUtil
import com.zhkj.txg.R
import com.zhkj.txg.base.CustomApplication
import com.zhkj.txg.module.group.ui.GroupDetailActivity
import com.zhkj.txg.module.home.entity.SearchProductEntity
import com.zhkj.txg.module.order.entity.*
import com.zhkj.txg.module.product.ui.ProductDetailActivity
import com.zhkj.txg.module.product.ui.ProductDetailActivity2
import com.zhkj.txg.utils.ImageUtils
import java.text.SimpleDateFormat
import java.util.*

class OrderDetailAdapter(
    onRefund: (entity: OrderListGoodsEntity) -> Unit
) : BaseNodeAdapter() {

    companion object {
        val TYPE_STATUS = 0
        val TYPE_SHIPPING = 1
        val TYPE_ADDRESS = 2
        val TYPE_SHOP = 3
        val TYPE_GOODS = 4
        val TYPE_PRICE = 5
        val TYPE_ORDER = 6
        val TYPE_GROUP = 7//拼团进度
    }

    init {
        addNodeProvider(OrderStatusProvider())
        addNodeProvider(OrderShippingProvider())
        addNodeProvider(OrderDetailAddressProvider())
        addNodeProvider(OrderDetailShopProvider())
        addNodeProvider(OrderDetailGoodsProvider(onRefund))
        addNodeProvider(OrderDetailDataProvider())
        addNodeProvider(OrderDetailPriceProvider())
        addNodeProvider(ProgressStatusProvider())
    }

    override fun getItemType(data: List<BaseNode>, position: Int): Int {
        return when (data[position]) {
            is OrderDetailStatusEntity -> TYPE_STATUS
            is OrderDetailUIShippingEntity -> TYPE_SHIPPING
            is OrderDetailUIAddressEntity -> TYPE_ADDRESS
            is OrderDetailUIShopEntity -> TYPE_SHOP
            is OrderListGoodsEntity -> TYPE_GOODS
            is OrderDetailPriceUIEntity -> TYPE_PRICE
            is OrderDetailUIEntity -> TYPE_ORDER
            is ProgressEntity -> TYPE_GROUP
            else -> -1
        }
    }
}

class OrderStatusProvider : BaseNodeProvider() {
    override val itemViewType: Int = OrderDetailAdapter.TYPE_STATUS
    override val layoutId: Int = R.layout.item_order_detail_status

    override fun convert(helper: BaseViewHolder, data: BaseNode) {
        (data as? OrderDetailStatusEntity)?.let {
            val textView = helper.setText(R.id.tvOrderDetailStatus, it.name)
                .getView<TextView>(R.id.tvOrderDetailStatus)
            if (it.drawableResId > 0) {
                val drawable = context.getDrawable(R.drawable.ic_order_status_wait_pay)
                drawable?.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
                textView.setCompoundDrawablesRelative(null, null, drawable, null)
            }
            helper.setGone(R.id.frameOrderDetailStatus, data.status != 1)
            helper.setGone(R.id.linearOrderDetailStatus, data.status == 1)
            if (data.status == 2) {
                val drawable = context.getDrawable(R.drawable.ic_1_beihuo_svg)
                drawable?.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
                val textOrderBeihuo: TextView = helper.getView<TextView>(R.id.textOrderBeihuo)
                textOrderBeihuo.setCompoundDrawablesRelative(null, drawable, null, null)
                textOrderBeihuo.setTextColor(context.resources.getColor(R.color.colorMain))
            } else if (data.status == 3) {
                val drawable = context.getDrawable(R.drawable.ic_1_ziti_svg)
                drawable?.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
                val textOrderZiqu: TextView = helper.getView<TextView>(R.id.textOrderZiqu)
                textOrderZiqu.setCompoundDrawablesRelative(null, drawable, null, null)
                textOrderZiqu.setTextColor(context.resources.getColor(R.color.colorMain))
            } else if (data.status == 4) {
                val drawable = context.getDrawable(R.drawable.ic_1_wancheng_svg)
                drawable?.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
                val textOrderWancheng: TextView = helper.getView<TextView>(R.id.textOrderWancheng)
                textOrderWancheng.setCompoundDrawablesRelative(null, drawable, null, null)
                textOrderWancheng.setTextColor(context.resources.getColor(R.color.colorMain))
            } else {
                val drawable1 = context.getDrawable(R.drawable.ic_2_beihuo_svg)
                drawable1?.setBounds(0, 0, drawable1.intrinsicWidth, drawable1.intrinsicHeight)
                val textOrderBeihuo: TextView = helper.getView<TextView>(R.id.textOrderBeihuo)
                textOrderBeihuo.setCompoundDrawablesRelative(null, drawable1, null, null)
                textOrderBeihuo.setTextColor(context.resources.getColor(R.color.colorLightText))

                val drawable2 = context.getDrawable(R.drawable.ic_2_ziti_svg)
                drawable2?.setBounds(0, 0, drawable2.intrinsicWidth, drawable2.intrinsicHeight)
                val textOrderZiqu: TextView = helper.getView<TextView>(R.id.textOrderZiqu)
                textOrderZiqu.setCompoundDrawablesRelative(null, drawable2, null, null)
                textOrderZiqu.setTextColor(context.resources.getColor(R.color.colorLightText))

                val drawable3 = context.getDrawable(R.drawable.ic_2_ziti_svg)
                drawable3?.setBounds(0, 0, drawable3.intrinsicWidth, drawable3.intrinsicHeight)
                val textOrderWancheng: TextView = helper.getView<TextView>(R.id.textOrderWancheng)
                textOrderWancheng.setCompoundDrawablesRelative(null, drawable3, null, null)
                textOrderWancheng.setTextColor(context.resources.getColor(R.color.colorLightText))
            }
//            if (data.status == 1) {
//                helper.setGone(R.id.frameOrderDetailStatus,if (data.status == 1) false else true)
//            } else {
//                helper.setGone(R.id.frameOrderDetailStatus,true)
//            }
        }
    }
}

class ProgressStatusProvider : BaseNodeProvider() {
    override val itemViewType: Int = OrderDetailAdapter.TYPE_GROUP
    override val layoutId: Int = R.layout.item_order_detail_progress
    private val sdf = SimpleDateFormat("HH:mm:ss")
    override fun convert(helper: BaseViewHolder, data: BaseNode) {
        sdf.timeZone = TimeZone.getTimeZone("GMT")
        (data as? ProgressEntity)?.let {
            val textView =
                helper.setText(R.id.text_people, "${it.people}人成团，当前有${it.actual_people}人成团")
                    .setText(
                        R.id.text_remain,
                        if (it.end_time > 0) "剩余${if (it.end_time > 86400) "${it.end_time / 86400}天" else ""}${sdf.format(
                            it.end_time * 1000
                        )}结束" else "拼团时间已到"
                    )
        }
    }
}

class OrderShippingProvider : BaseNodeProvider() {
    override val itemViewType: Int = OrderDetailAdapter.TYPE_SHIPPING
    override val layoutId: Int = R.layout.item_order_detail_shipping

    override fun convert(helper: BaseViewHolder, data: BaseNode) {
        (data as? OrderDetailUIShippingEntity)?.let {
            helper.setText(R.id.tvOrderDetailShipping, "暂无物流信息")
                .setText(R.id.tvOrderDetailShippingTime, it.shippingTime)
                .setGone(R.id.tvOrderDetailShippingTime, it.shippingNumber.isEmpty())
        }
    }
}

class OrderDetailAddressProvider : BaseNodeProvider() {
    override val itemViewType: Int = OrderDetailAdapter.TYPE_ADDRESS
    override val layoutId: Int = R.layout.item_order_detail_address

    override fun convert(helper: BaseViewHolder, data: BaseNode) {
        (data as? OrderDetailUIAddressEntity)?.let {
            helper.setText(R.id.tvOrderDetailConsignee, it.consignee)
                .setText(R.id.tvOrderDetailMobile, it.mobile)
                .setText(R.id.tvOrderDetailAddress, it.address)
        }
    }
}

class OrderDetailShopProvider : BaseNodeProvider() {
    override val itemViewType: Int = OrderDetailAdapter.TYPE_SHOP
    override val layoutId: Int = R.layout.item_order_detail_shop

    override fun convert(helper: BaseViewHolder, data: BaseNode) {
        (data as? OrderDetailUIShopEntity)?.let {
            val imageView = helper.setText(R.id.tvOrderDetailShop, it.shopName)
                .getView<ImageView>(R.id.ivOrderDetailShop)
            ImageUtils.loadCircleImage(context, imageView, it.shopImg)
        }
    }
}

class OrderDetailGoodsProvider(
    private val onRefund: (entity: OrderListGoodsEntity) -> Unit
) : BaseNodeProvider() {
    override val itemViewType: Int = OrderDetailAdapter.TYPE_GOODS
    override val layoutId: Int = R.layout.item_order_goods_2

    private var padding: Int = 0

    init {
        padding = ScreenUtil.dip(BaseApplication.INSTANCE, 20)
        addChildClickViewIds(R.id.tvOrderGoodsRefund)
    }

    override fun convert(helper: BaseViewHolder, data: BaseNode) {
//        helper.itemView.setP(padding, padding, padding, 0)
        (data as? OrderListGoodsEntity)?.let {
            val imageView = helper.setText(R.id.tvOrderGoodsName, it.goodsName)
                .setText(R.id.tvOrderGoodsPrice, SpanUtil.setMoneySymbol(it.goodsPrice))
                .setText(R.id.tvOrderGoodsNumber, "x${it.goodsNum}")
//                .setGone(R.id.tvOrderGoodsRefund, !it.showRefundButton)
//                .setText(R.id.tvOrderGoodsRefund, getRefundStatus(it.refundStatus))
                .getView<ImageView>(R.id.ivOrderGoods)
            ImageUtils.loadRoundCornerImage(context, imageView, it.image)
        }
    }

    override fun onChildClick(helper: BaseViewHolder, view: View, data: BaseNode, position: Int) {
        if (view.id == R.id.tvOrderGoodsRefund) (data as? OrderListGoodsEntity)?.let { onRefund(it) }
    }

    override fun onClick(helper: BaseViewHolder, view: View, data: BaseNode, position: Int) {
        (data as? OrderListGoodsEntity)?.let {
//            ProductDetailActivity2.normalProduct(context, data.goodsId.toInt(), shopId = CustomApplication.getShopId())
//            if (!it.isGroup) {
//                ProductDetailActivity.normalProduct(context, it.goodsId.toInt())
//            } else {
//                GroupDetailActivity.normalProduct(context, it.goodsId.toString())
//            }
        }
    }

    private fun getRefundStatus(refundStatus: Int): String = when (refundStatus) {
        1 -> BaseApplication.INSTANCE.getString(R.string.refund_status_ing)
        2 -> BaseApplication.INSTANCE.getString(R.string.refund_status_done)
        3 -> BaseApplication.INSTANCE.getString(R.string.refund_status_fail)
        4 -> BaseApplication.INSTANCE.getString(R.string.refund_status_back)
        else -> BaseApplication.INSTANCE.getString(R.string.order_refund)
    }
}

class OrderDetailPriceProvider : BaseNodeProvider() {
    override val itemViewType: Int = OrderDetailAdapter.TYPE_PRICE
    override val layoutId: Int = R.layout.item_order_detail_price

    override fun convert(helper: BaseViewHolder, data: BaseNode) {
        (data as? OrderDetailPriceUIEntity)?.let {
            helper.setText(R.id.tvOrderDetailFreight, SpanUtil.setMoneySymbol(it.fright))
                .setText(R.id.tvOrderDetailCoupon, SpanUtil.setMoneySymbol(it.couponPrice))
                .setText(R.id.tvOrderDetailRealPrice, SpanUtil.setMoneySymbol(it.totalAmount))
        }
    }
}

class OrderDetailDataProvider : BaseNodeProvider() {

    private val infoAdapter = OrderDetailInfoAdapter()

    override val itemViewType: Int = OrderDetailAdapter.TYPE_ORDER
    override val layoutId: Int = R.layout.item_order_detail_data

    override fun convert(helper: BaseViewHolder, data: BaseNode) {
        (data as? OrderDetailUIEntity)?.let {
            infoAdapter.replaceData(it.orderInfo)
        }
    }

    override fun onViewHolderCreated(viewHolder: BaseViewHolder, viewType: Int) {
        with(viewHolder.getView<RecyclerView>(R.id.rvOrderDetailInfo)) {
            layoutManager = LinearLayoutManager(context)
            adapter = infoAdapter
        }
    }
}